package com.starpl.mobile.android.monthunsae.november.handler;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.text.Html;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.DayInfo;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsae;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsaeDay;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.component.BaseGridHandler;

public class CalendarGridHandler extends BaseGridHandler
{
	private final int LOVE_UNSAE = 10084402;
	private final int MONEY_UNSAE = 10001374;
	
	private ArrayList<DayInfo> mList;
	private Calendar mCalendar;
	private int mKeywordIdx;
	
	private MonthUnsae mMonthUnsae;
	private ArrayList<MonthUnsaeDay> mMonthUnsaeDay;
	
	/**
	 * @param activity
	 * @param keywordIdx
	 * @param calendar
	 */
	public CalendarGridHandler(Activity activity, int keywordIdx, Calendar calendar)
	{
		super(activity);
		this.mList = new ArrayList<DayInfo>();
		this.mKeywordIdx = keywordIdx;
		this.mCalendar = calendar;
		
		this.mMonthUnsae = (MonthUnsae) BaseApplication.getGlobalPersistent("monthUnsae");
		this.mMonthUnsaeDay = this.mMonthUnsae.getMonthUnsaeDayList();
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#getArrayList()
	 */
	@Override
	public ArrayList<?> getArrayList()
	{
		return mList;
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#getRowView(int, android.view.View)
	 */
	@Override
	public View getRowView(int position, View convertView)
	{
		DayInfo day = mList.get(position);

		DayViewHolde dayViewHolder;
		
		if(convertView == null)
		{
			convertView = inflate(R.layout.day_cell);

			if(position % 7 == 6)
			{
				convertView.setLayoutParams(new GridView.LayoutParams(getCellWidthDP() + getRestCellWidthDP(),
						getCellHeightDP()));
			}
			else
			{
				convertView.setLayoutParams(new GridView.LayoutParams(getCellWidthDP(), getCellHeightDP()));
			}

			dayViewHolder = new DayViewHolde();

			dayViewHolder.llBackground = (LinearLayout) convertView.findViewById(R.id.day_cell_ll_background);
			dayViewHolder.tvDay = (TextView) convertView.findViewById(R.id.day_cell_tv_date);
			dayViewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.day_cell_iv_icon);

			convertView.setTag(dayViewHolder);
		}
		else
		{
			dayViewHolder = (DayViewHolde) convertView.getTag();
		}
		
		if(day != null)
		{
			// 날짜 셋팅
			if(day.isInMonth())
			{
				Calendar calendar = Calendar.getInstance();
				String today = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
				
				// 오늘날짜에 대해선 배경색을 다르게 처리한다.
				if(day.getDate().equals(today))
				{
					int monthNumber = Integer.parseInt(BaseApplication.getConfigPersistent("monthNumber")) - 1;
					Calendar monthCal = Calendar.getInstance();
					monthCal.set(Calendar.MONTH, monthNumber);

					if(monthCal.get(Calendar.MONTH) == calendar.get(Calendar.MONTH))
					{
						// fff8d6
						dayViewHolder.llBackground.setBackgroundResource(R.drawable.bg_calendar_today);
					}
					else
					{
						dayViewHolder.llBackground.setBackgroundResource(R.drawable.bg_calendar_middle);
					}
				}
				else
				{
					dayViewHolder.llBackground.setBackgroundResource(R.drawable.bg_calendar_middle);
				}
				
				// 일요일에 대한 글씨색 세팅
				if(position % 7 == 0)
				{
					String dayText = "<font color='#E44044'>" + day.getDate() + "</font>";
					dayViewHolder.tvDay.setText(Html.fromHtml(dayText));
				}
				// 평일에 대한 글씨색 세팅
				else
				{
					String dayText = "<font color='#4D4D4D'>" + day.getDate() + "</font>";
					dayViewHolder.tvDay.setText(Html.fromHtml(dayText));
				}
			}
			// 이번달이 아닌 날짜에 대한 글씨색 세팅
			else
			{
				String dayText = "<font color='#B3B3B3'>" + day.getDate() + "</font>";
				dayViewHolder.tvDay.setText(Html.fromHtml(dayText));
			}
			
			// 아이콘 세팅
			dayViewHolder.ivIcon.setBackgroundResource(day.getIcon());
		}

		
		return convertView;
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#onRowClick(android.view.View)
	 */
	@Override
	public void onRowClick(View v)
	{

	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#onRowClear()
	 */
	@Override
	public void onRowClear()
	{
		mList.clear();
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#loadContent()
	 */
	@Override
	public void loadContent()
	{
		int lastMonthStartDay;
		int dayOfMonth;
		int thisMonthLastDay;

		mList.clear();

		// 이번달 시작일의 요일을 구한다. 시작일이 일요일인 경우 인덱스를 1(일요일)에서 8(다음주 일요일)로 바꾼다.)
		dayOfMonth = mCalendar.get(Calendar.DAY_OF_WEEK);
		thisMonthLastDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		mCalendar.add(Calendar.MONTH, -1);

		// 지난달의 마지막 일자를 구한다.
		lastMonthStartDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		mCalendar.add(Calendar.MONTH, 1);

		// 일요일로 시작하는 달에 대한 처리
		if(dayOfMonth == 1)
		{
			dayOfMonth += 7;
		}

		lastMonthStartDay -= (dayOfMonth - 1) - 1;

		DayInfo day;

		for(int i = 0 ; i < dayOfMonth - 1 ; i++)
		{
			int date = lastMonthStartDay + i;
			day = new DayInfo();
			day.setDate(Integer.toString(date));
			day.setInMonth(false);

			mList.add(day);
		}
		for(int i = 1 ; i <= thisMonthLastDay ; i++)
		{
			MonthUnsaeDay monthUnsaeDay = mMonthUnsaeDay.get(i-1);
			
			if(mKeywordIdx == LOVE_UNSAE)
			{
				day = new DayInfo(monthUnsaeDay, 1);
				day.setDate(Integer.toString(i));
				day.setInMonth(true);
				mList.add(day);
			}
			else if(mKeywordIdx == MONEY_UNSAE)
			{
				day = new DayInfo(monthUnsaeDay, 2);
				day.setDate(Integer.toString(i));
				day.setInMonth(true);
				mList.add(day);
			}
		}
		for(int i = 1 ; i < 42 - (thisMonthLastDay + dayOfMonth - 1) + 1 ; i++)
		{
			day = new DayInfo();
			day.setDate(Integer.toString(i));
			day.setInMonth(false);
			mList.add(day);
		}
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.component.BaseGridHandler#onInit()
	 */
	@Override
	public void onInit()
	{
		mGridView.setNumColumns(7);
		mGridView.setLayoutParams(new GridView.LayoutParams(mParentActivity.getResources().getDisplayMetrics().widthPixels, mParentActivity.getResources().getDisplayMetrics().widthPixels/7*6));
		
		setAdapter();
		loadContent();
	}


	/**
	 * @author croute
	 *
	 */
	public class DayViewHolde
	{
		public LinearLayout llBackground;
		public TextView tvDay;
		public ImageView ivIcon;

	}

	/**
	 * @return
	 */
	private int getCellWidthDP()
	{
		int width = mParentActivity.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width / 7;

		return cellWidth;
	}

	/**
	 * @return
	 */
	private int getRestCellWidthDP()
	{
		int width = mParentActivity.getResources().getDisplayMetrics().widthPixels;
		int cellWidth = width % 7;

		return cellWidth;
	}

	/**
	 * @return
	 */
	private int getCellHeightDP()
	{
		int width = mParentActivity.getResources().getDisplayMetrics().widthPixels;
		int cellHeight = (width / 7);

		return cellHeight;
	}

}
